<template>
  <div class="app-container">
    <div>支付记录页面</div>
    <div class="filter-container">
      <el-input v-model="listQuery.title" placeholder="Title" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        Search
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="下单用户" align="center" prop="userId">
        <el-table-column label="购买人" width="100px">
          <template slot-scope="{row}">
            <span>{{ row.userAccount.userName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号" width="110px">
          <template slot-scope="{row}">
            <span>{{ row.userAccount.userMobile }}</span>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="下单卡快照">
        <el-table-column label="卡样式" width="80px" align="center">
          <template slot-scope="{row}">
            <el-tag>{{ row.cardStyle | cardStyleEnum }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="卡名称" width="120px" prop="cardName" />
        <el-table-column label="卡次数" width="70px" prop="cardTimes" />
        <el-table-column label="卡分钟" width="70px" prop="cardMinutes" />
        <el-table-column label="卡价格" width="70px" prop="cardPrice" />
      </el-table-column>
      <el-table-column label="订单信息">
        <el-table-column label="创建时间" width="150px" align="center">
          <template slot-scope="{row}">
            <span>{{ new Date(row.createDatetime) | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="支付状态" width="100px">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="showPaymentRecord(row)">{{ row.paymentStatus | paymentStatusEnum }}</el-button>
          </template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="服务卡ID" width="150px" prop="cardId">
        <template slot-scope="{row}">
          <span>{{ row.cardId || '-' }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.current" :limit.sync="listQuery.size" @pagination="getList" />

    <el-dialog title="支付记录" :visible.sync="paymentRecordDialogVisible" width="70%">
      <el-table :data="paymentRecords">
        <el-table-column label="支付金额" width="100px" prop="paymentAmount" />
        <el-table-column label="支付状态" width="100px" prop="paymentStatus">
          <template slot-scope="{row}">
            <el-tag>{{ row.paymentStatus | paymentStatusEnum }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="三方类型" width="100px" prop="thirdPayType" />
        <el-table-column label="三方下单ID" width="350px" prop="thirdPayId" />
        <el-table-column label="三方交易ID" width="350px" prop="thirdTransactionId" />
        <el-table-column label="三方退款ID" width="350px" prop="thirdRefundId" />
        <el-table-column label="错误码" width="100px" prop="errorCode" />
        <el-table-column label="错误消息" width="200px" prop="errorMessage" />
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogBindReceiverVisible = false">取消</el-button>
        <el-button type="primary" @click="modifyData()">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from '@/api/card/order'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'PayRecord',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    cardStyleEnum(cardStyle) {
      const cardStyleMap = {
        NORMAL: '普通卡',
        SILVER: '白银卡',
        GOLD: '黄金卡',
        PLATINUM: '铂金卡',
        BLACK: '黑金卡',
        DIAMOND: '钻石卡'
      }
      return cardStyleMap[cardStyle] || '-'
    },
    paymentStatusEnum(paymentStatus) {
      const paymentStatusMap = {
        PENDING: '待支付',
        PAID: '已支付',
        FAILED: '支付失败',
        REFUNDING: '退款中',
        REFUNDED: '已退款',
        REFUND_FAILED: '退款失败'
      }
      return paymentStatusMap[paymentStatus] || '-'
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        with: 'USER_ACCOUNT,PAYMENT_RECORD',
        current: 1,
        size: 20,
        importance: undefined,
        title: undefined,
        type: undefined,
        sort: '+id'
      },
      pvData: [],
      paymentRecords: [],
      paymentRecordDialogVisible: false,
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total

        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    showPaymentRecord(row) {
      this.paymentRecords = row.paymentRecordList
      this.paymentRecordDialogVisible = true
    }
  }
}
</script>
